﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace DotNet.Common
{
    /// <summary>
    /// 缓存常用操作
    /// </summary>
    public class CacheUtil
    {
        public CacheUtil()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 增加一个缓存对象
        /// </summary>
        /// <param name="strKey">键</param>
        /// <param name="valueObj">值</param>
        /// <param name="duration">以秒为单位</param>
        /// <returns></returns>
        public static bool Insert(string strKey, object valueObj, double durationSecond)
        {
            if (strKey != null && strKey.Length != 0 && valueObj != null)
            {
                //建立回调委托的一个实例

                CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
                HttpContext.Current.Cache.Insert(strKey, valueObj, null,DateTime.Now.AddSeconds(durationSecond),System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Default,
                callBack);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断缓存对象是否存在
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static bool IsExist(string strKey)
        {
            return HttpContext.Current.Cache[strKey] != null;
        }

        /// <summary>
        /// 读取缓存对象
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object Read(string strKey)
        {
            //取出值

            if (HttpContext.Current.Cache[strKey] != null)
            {
                object obj = HttpContext.Current.Cache[strKey];
                if (obj == null)
                {
                    return null;
                }
                else
                {
                    return obj;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除缓存对象
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static void Remove(string strKey)
        {
            if (HttpContext.Current.Cache[strKey] != null)
            {
                HttpContext.Current.Cache.Remove(strKey);
            }
        }

        /// <summary>
        /// 根据设置的正则表达式清除缓存对象
        /// </summary>
        /// <param name="pattern">正则表达式</param>
        public static void RemoveByRegexp(string pattern)
        {
            if (pattern != "")
            {
                IDictionaryEnumerator enu = HttpContext.Current.Cache.GetEnumerator();
                while (enu.MoveNext())
                {
                    string key = enu.Key.ToString();
                    if(Regex.IsMatch(key,pattern))
                    {
                        Remove(key);
                    }
                }
            }
        }

        /// <summary>
        /// 清除所有缓存对象
        /// </summary>
        public static void Clear()
        {
            IDictionaryEnumerator enu = HttpContext.Current.Cache.GetEnumerator();
            while (enu.MoveNext())
            {
                Remove(enu.Key.ToString());
            }
        }

        /// <summary>
        /// 此方法在值失效之前调用，可以用于在失效之前更新数据库，或从数据库重新获取数据
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="obj"></param>
        /// <param name="reason"></param>
        private static void onRemove(string strKey, object obj, CacheItemRemovedReason reason)
        {

        }
    }
}
